package com.heima.model.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.article.mapper.ApArticleConfigMapper;
import com.heima.model.common.apArticle.pojos.ApArticleConfig;
import org.apache.avro.data.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class DownOrUpListener {
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void getMessage(String message){
        if(message!=null) {
            //1.转换类型格式
            Map map = JSON.parseObject(message, Map.class);
            //2.获取属性值
            Object articleId = map.get("articleId");

            /**
             * enable=0下架
             * enable=1上架
             * 目前apArticleConfig表中的字段is_down
             *      true:下架
             *      false：没有下架（上架） 0
             *
             */
            Integer enable = (Integer) map.get("enable");
            //同步属性值
            ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(
                    Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, articleId)
            );
            if (apArticleConfig != null) {
                //修改状态
                apArticleConfig.setIsDown(enable == 1 ? false : true);
                apArticleConfigMapper.updateById(apArticleConfig);
            }
        }
    }
}
